<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- /fasttmp/mkdist-qt-4.3.5-1211793125/qtopia-core-opensource-src-4.3.5/doc/src/examples/activeqt/qutlook.qdoc -->
<head>
  <title>Qt 4.3: Qutlook Example (ActiveQt)</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://www.trolltech.com/products/qt"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="modules.html"><font color="#004faf">Modules</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
<td align="right" valign="top" width="230"><a href="http://www.trolltech.com"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></a></td></tr></table><h1 align="center">Qutlook Example (ActiveQt)<br /><small></small></h1>
<p>Files:</p>
<ul>
<li><a href="activeqt-qutlook-addressview-cpp.html">activeqt/qutlook/addressview.cpp</a></li>
<li><a href="activeqt-qutlook-addressview-h.html">activeqt/qutlook/addressview.h</a></li>
<li><a href="activeqt-qutlook-main-cpp.html">activeqt/qutlook/main.cpp</a></li>
</ul>
<p>The Qutlook example demonstrates the use of <a href="activeqt.html#activeqt">ActiveQt</a> to automate Outlook. The example makes use of the <a href="activeqt-dumpcpp.html#dumpcpp">dumpcpp</a> tool to generate a C++ namespace for the type library describing the Outlook Object Model.</p>
<p>The project file for the example looks like this:</p>
<pre> TEMPLATE = app
 TARGET   = qutlook
 CONFIG  += qaxcontainer

 TYPELIBS = $$system(dumpcpp -getfile {00062FFF-0000-0000-C000-000000000046})

 isEmpty(TYPELIBS) {
     message(&quot;Microsoft Outlook type library not found!&quot;)
     REQUIRES += Outlook
 } else {
     HEADERS  = addressview.h
     SOURCES  = addressview.cpp main.cpp
 }</pre>
<p>The project file uses the <tt>dumpcpp</tt> tool to add an MS Outlook type library to the project. If this fails, then the generated makefile will just print an error message, otherwise the build step will now run the <i>dumpcpp</i> tool on the type library, and generate a header and a cpp file (in this case, <tt>msoutl.h</tt> and <tt>msoutl.cpp</tt>) that declares and implement an easy to use API to the Outlook objects.</p>
<pre> class AddressView : public QWidget
 {
     Q_OBJECT

 public:
     AddressView(QWidget *parent = 0);

 protected slots:
     void addEntry();
     void changeEntry();
     void itemSelected(const QModelIndex &amp;index);

     void updateOutlook();

 protected:
     AddressBookModel *model;

     QTreeView *treeView;
     QPushButton *add, *change;
     QLineEdit *iFirstName, *iLastName, *iAddress, *iEMail;
 };</pre>
<p>The AddressView class is a <a href="qwidget.html">QWidget</a> subclass for the user interface. The <a href="qtreeview.html">QTreeView</a> widget will display the contents of Outlook's Contact folder as provided by the <tt>model</tt>.</p>
<pre> #include &quot;addressview.h&quot;
 #include &quot;msoutl.h&quot;
 #include &lt;QtGui&gt;

 class AddressBookModel : public QAbstractListModel
 {
 public:
     AddressBookModel(AddressView *parent);
     ~AddressBookModel();

     int rowCount(const QModelIndex &amp;parent = QModelIndex()) const;
     int columnCount(const QModelIndex &amp;parent) const;
     QVariant headerData(int section, Qt::Orientation orientation, int role) const;
     QVariant data(const QModelIndex &amp;index, int role) const;

     void changeItem(const QModelIndex &amp;index, const QString &amp;firstName, const QString &amp;lastName, const QString &amp;address, const QString &amp;email);
     void addItem(const QString &amp;firstName, const QString &amp;lastName, const QString &amp;address, const QString &amp;email);
     void update();

 private:
     Outlook::Application outlook;
     Outlook::Items * contactItems;

     mutable QHash&lt;QModelIndex, QStringList&gt; cache;
 };</pre>
<p>The AddressBookModel class is a <a href="qabstractlistmodel.html">QAbstractListModel</a> subclass that communicates directly with Outlook, using a <a href="qhash.html">QHash</a> for caching.</p>
<pre> AddressBookModel::AddressBookModel(AddressView *parent)
 : QAbstractListModel(parent)
 {
     if (!outlook.isNull()) {
         Outlook::NameSpace session(outlook.Session());
         session.Logon();
         Outlook::MAPIFolder *folder = session.GetDefaultFolder(Outlook::olFolderContacts);
         contactItems = new Outlook::Items(folder-&gt;Items());
         connect(contactItems, SIGNAL(ItemAdd(IDispatch*)), parent, SLOT(updateOutlook()));
         connect(contactItems, SIGNAL(ItemChange(IDispatch*)), parent, SLOT(updateOutlook()));
         connect(contactItems, SIGNAL(ItemRemove()), parent, SLOT(updateOutlook()));

         delete folder;
     }
 }</pre>
<p>The constructor initializes Outlook. The various signals Outlook provides to notify about contents changes are connected to the <tt>updateOutlook()</tt> slot.</p>
<pre> AddressBookModel::~AddressBookModel()
 {
     delete contactItems;

     if (!outlook.isNull())
         Outlook::NameSpace(outlook.Session()).Logoff();
 }</pre>
<p>The destructor logs off from the session.</p>
<pre> int AddressBookModel::rowCount(const QModelIndex &amp;) const
 {
     return contactItems ? contactItems-&gt;Count() : 0;
 }

 int AddressBookModel::columnCount(const QModelIndex &amp;parent) const
 {
     return 4;
 }</pre>
<p>The <tt>rowCount()</tt> implementation returns the number of entries as reported by Outlook. <tt>columnCount</tt> and <tt>headerData</tt> are implemented to show four columns in the tree view.</p>
<pre> QVariant AddressBookModel::headerData(int section, Qt::Orientation orientation, int role) const
 {
     if (role != Qt::DisplayRole)
         return QVariant();

     switch (section) {
     case 0:
         return tr(&quot;First Name&quot;);
     case 1:
         return tr(&quot;Last Name&quot;);
     case 2:
         return tr(&quot;Address&quot;);
     case 3:
         return tr(&quot;Email&quot;);
     default:
         break;
     }

     return QVariant();
 }</pre>
<p>The <tt>headerData()</tt> implementation returns hardcoded strings.</p>
<pre> QVariant AddressBookModel::data(const QModelIndex &amp;index, int role) const
 {
     if (!index.isValid() || role != Qt::DisplayRole)
         return QVariant();

     QStringList data;
     if (cache.contains(index)) {
         data = cache.value(index);
     } else {
         Outlook::ContactItem contact(contactItems-&gt;Item(index.row() + 1));
         data &lt;&lt; contact.FirstName() &lt;&lt; contact.LastName() &lt;&lt; contact.HomeAddress() &lt;&lt; contact.Email1Address();
         cache.insert(index, data);
     }

     if (index.column() &lt; data.count())
         return data.at(index.column());

     return QVariant();
 }</pre>
<p>The <tt>data()</tt> implementation is the core of the model. If the requested data is in the cache the cached value is used, otherwise the data is acquired from Outlook.</p>
<pre> void AddressBookModel::changeItem(const QModelIndex &amp;index, const QString &amp;firstName, const QString &amp;lastName, const QString &amp;address, const QString &amp;email)
 {
     Outlook::ContactItem item(contactItems-&gt;Item(index.row() + 1));

     item.SetFirstName(firstName);
     item.SetLastName(lastName);
     item.SetHomeAddress(address);
     item.SetEmail1Address(email);

     item.Save();

     cache.take(index);
 }</pre>
<p>The <tt>changeItem()</tt> slot is called when the user changes the current entry using the user interface. The Outlook item is accessed using the Outlook API, and is modified using the property setters. Finally, the item is saved to Outlook, and removed from the cache. Note that the model does not signal the view of the data change, as Outlook will emit a signal on it's own.</p>
<pre> void AddressBookModel::addItem(const QString &amp;firstName, const QString &amp;lastName, const QString &amp;address, const QString &amp;email)
 {
     Outlook::ContactItem item(outlook.CreateItem(Outlook::olContactItem));
     if (!item.isNull()) {
         item.SetFirstName(firstName);
         item.SetLastName(lastName);
         item.SetHomeAddress(address);
         item.SetEmail1Address(email);

         item.Save();
     }
 }</pre>
<p>The <tt>addItem()</tt> slot calls the CreateItem method of Outlook to create a new contact item, sets the properties of the new item to the values entered by the user and saves the item.</p>
<pre> void AddressBookModel::update()
 {
     cache.clear();

     emit reset();
 }</pre>
<p>The <tt>update()</tt> slot clears the cache, and emits the <a href="qtextstream.html#reset">reset</a>() signal to notify the view about the data change requiring a redraw of the contents.</p>
<pre> AddressView::AddressView(QWidget *parent)
 : QWidget(parent)
 {
     QGridLayout *mainGrid = new QGridLayout(this);

     QLabel *liFirstName = new QLabel(&quot;First &amp;Name&quot;, this);
     liFirstName-&gt;resize(liFirstName-&gt;sizeHint());
     mainGrid-&gt;addWidget(liFirstName, 0, 0);

     QLabel *liLastName = new QLabel(&quot;&amp;Last Name&quot;, this);
     liLastName-&gt;resize(liLastName-&gt;sizeHint());
     mainGrid-&gt;addWidget(liLastName, 0, 1);

     QLabel *liAddress = new QLabel(&quot;Add&amp;ress&quot;, this);
     liAddress-&gt;resize(liAddress-&gt;sizeHint());
     mainGrid-&gt;addWidget(liAddress, 0, 2);

     QLabel *liEMail = new QLabel(&quot;&amp;E-Mail&quot;, this);
     liEMail-&gt;resize(liEMail-&gt;sizeHint());
     mainGrid-&gt;addWidget(liEMail, 0, 3);

     add = new QPushButton(&quot;A&amp;dd&quot;, this);
     add-&gt;resize(add-&gt;sizeHint());
     mainGrid-&gt;addWidget(add, 0, 4);
     connect(add, SIGNAL(clicked()), this, SLOT(addEntry()));

     iFirstName = new QLineEdit(this);
     iFirstName-&gt;resize(iFirstName-&gt;sizeHint());
     mainGrid-&gt;addWidget(iFirstName, 1, 0);
     liFirstName-&gt;setBuddy(iFirstName);

     iLastName = new QLineEdit(this);
     iLastName-&gt;resize(iLastName-&gt;sizeHint());
     mainGrid-&gt;addWidget(iLastName, 1, 1);
     liLastName-&gt;setBuddy(iLastName);

     iAddress = new QLineEdit(this);
     iAddress-&gt;resize(iAddress-&gt;sizeHint());
     mainGrid-&gt;addWidget(iAddress, 1, 2);
     liAddress-&gt;setBuddy(iAddress);

     iEMail = new QLineEdit(this);
     iEMail-&gt;resize(iEMail-&gt;sizeHint());
     mainGrid-&gt;addWidget(iEMail, 1, 3);
     liEMail-&gt;setBuddy(iEMail);

     change = new QPushButton(&quot;&amp;Change&quot;, this);
     change-&gt;resize(change-&gt;sizeHint());
     mainGrid-&gt;addWidget(change, 1, 4);
     connect(change, SIGNAL(clicked()), this, SLOT(changeEntry()));

     treeView = new QTreeView(this);
     treeView-&gt;setSelectionMode(QTreeView::SingleSelection);
     treeView-&gt;setRootIsDecorated(false);

     model = new AddressBookModel(this);
     treeView-&gt;setModel(model);

     connect(treeView-&gt;selectionModel(), SIGNAL(currentChanged(QModelIndex, QModelIndex)), this, SLOT(itemSelected(QModelIndex)));

     mainGrid-&gt;addWidget(treeView, 2, 0, 1, 5);
 }

 void AddressView::updateOutlook()
 {
     model-&gt;update();
 }

 void AddressView::addEntry()
 {
     if (!iFirstName-&gt;text().isEmpty() || !iLastName-&gt;text().isEmpty() ||
          !iAddress-&gt;text().isEmpty() || !iEMail-&gt;text().isEmpty()) {
         model-&gt;addItem(iFirstName-&gt;text(), iFirstName-&gt;text(), iAddress-&gt;text(), iEMail-&gt;text());
     }

     iFirstName-&gt;setText(&quot;&quot;);
     iLastName-&gt;setText(&quot;&quot;);
     iAddress-&gt;setText(&quot;&quot;);
     iEMail-&gt;setText(&quot;&quot;);
 }

 void AddressView::changeEntry()
 {
     QModelIndex current = treeView-&gt;currentIndex();

     if (current.isValid())
         model-&gt;changeItem(current, iFirstName-&gt;text(), iLastName-&gt;text(), iAddress-&gt;text(), iEMail-&gt;text());
 }

 void AddressView::itemSelected(const QModelIndex &amp;index)
 {
     if (!index.isValid())
         return;

     QAbstractItemModel *model = treeView-&gt;model();
     iFirstName-&gt;setText(model-&gt;data(model-&gt;index(index.row(), 0)).toString());
     iLastName-&gt;setText(model-&gt;data(model-&gt;index(index.row(), 1)).toString());
     iAddress-&gt;setText(model-&gt;data(model-&gt;index(index.row(), 2)).toString());
     iEMail-&gt;setText(model-&gt;data(model-&gt;index(index.row(), 3)).toString());
 }</pre>
<p>The rest of the file implements the user interface using only Qt APIs, i.e&#x2e; without communicating with Outlook directly.</p>
<pre> #include &quot;addressview.h&quot;
 #include &lt;QApplication&gt;

 int main(int argc, char ** argv)
 {
     QApplication a(argc, argv);

     AddressView view;
     view.setWindowTitle(&quot;Qt Example - Looking at Outlook&quot;);
     view.show();

     return a.exec();
 }</pre>
<p>The <tt>main()</tt> entry point function finally instantiates the user interface and enters the event loop.</p>
<p>To build the example you must first build the <a href="qaxcontainer.html">QAxContainer</a> library. Then run your make tool in <tt>examples/activeqt/qutlook</tt> and run the resulting <tt>qutlook.exe</tt>.</p>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2008 <a href="trolltech.html">Trolltech</a></td>
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt 4.3.5</div></td>
</tr></table></div></address></body>
</html>
